{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## 5.13 \u83b7\u53d6\u6587\u4ef6\u5939\u4e2d\u7684\u6587\u4ef6\u5217\u8868\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u95ee\u9898\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4f60\u60f3\u83b7\u53d6\u6587\u4ef6\u7cfb\u7edf\u4e2d\u67d0\u4e2a\u76ee\u5f55\u4e0b\u7684\u6240\u6709\u6587\u4ef6\u5217\u8868\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u89e3\u51b3\u65b9\u6848\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4f7f\u7528 os.listdir() \u51fd\u6570\u6765\u83b7\u53d6\u67d0\u4e2a\u76ee\u5f55\u4e2d\u7684\u6587\u4ef6\u5217\u8868\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "import os\nnames = os.listdir('somedir')"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u7ed3\u679c\u4f1a\u8fd4\u56de\u76ee\u5f55\u4e2d\u6240\u6709\u6587\u4ef6\u5217\u8868\uff0c\u5305\u62ec\u6240\u6709\u6587\u4ef6\uff0c\u5b50\u76ee\u5f55\uff0c\u7b26\u53f7\u94fe\u63a5\u7b49\u7b49\u3002\n\u5982\u679c\u4f60\u9700\u8981\u901a\u8fc7\u67d0\u79cd\u65b9\u5f0f\u8fc7\u6ee4\u6570\u636e\uff0c\u53ef\u4ee5\u8003\u8651\u7ed3\u5408 os.path \u5e93\u4e2d\u7684\u4e00\u4e9b\u51fd\u6570\u6765\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u3002\u6bd4\u5982\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "import os.path\n\n# Get all regular files\nnames = [name for name in os.listdir('somedir')\n        if os.path.isfile(os.path.join('somedir', name))]\n\n# Get all dirs\ndirnames = [name for name in os.listdir('somedir')\n        if os.path.isdir(os.path.join('somedir', name))]"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5b57\u7b26\u4e32\u7684 startswith() \u548c endswith() \u65b9\u6cd5\u5bf9\u4e8e\u8fc7\u6ee4\u4e00\u4e2a\u76ee\u5f55\u7684\u5185\u5bb9\u4e5f\u662f\u5f88\u6709\u7528\u7684\u3002\u6bd4\u5982\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "pyfiles = [name for name in os.listdir('somedir')\n            if name.endswith('.py')]"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5bf9\u4e8e\u6587\u4ef6\u540d\u7684\u5339\u914d\uff0c\u4f60\u53ef\u80fd\u4f1a\u8003\u8651\u4f7f\u7528 glob \u6216 fnmatch \u6a21\u5757\u3002\u6bd4\u5982\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "import glob\npyfiles = glob.glob('somedir/*.py')\n\nfrom fnmatch import fnmatch\npyfiles = [name for name in os.listdir('somedir')\n            if fnmatch(name, '*.py')]"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u8ba8\u8bba\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u83b7\u53d6\u76ee\u5f55\u4e2d\u7684\u5217\u8868\u662f\u5f88\u5bb9\u6613\u7684\uff0c\u4f46\u662f\u5176\u8fd4\u56de\u7ed3\u679c\u53ea\u662f\u76ee\u5f55\u4e2d\u5b9e\u4f53\u540d\u5217\u8868\u800c\u5df2\u3002\n\u5982\u679c\u4f60\u8fd8\u60f3\u83b7\u53d6\u5176\u4ed6\u7684\u5143\u4fe1\u606f\uff0c\u6bd4\u5982\u6587\u4ef6\u5927\u5c0f\uff0c\u4fee\u6539\u65f6\u95f4\u7b49\u7b49\uff0c\n\u4f60\u6216\u8bb8\u8fd8\u9700\u8981\u4f7f\u7528\u5230 os.path \u6a21\u5757\u4e2d\u7684\u51fd\u6570\u6216\u7740 os.stat() \u51fd\u6570\u6765\u6536\u96c6\u6570\u636e\u3002\u6bd4\u5982\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "# Example of getting a directory listing\n\nimport os\nimport os.path\nimport glob\n\npyfiles = glob.glob('*.py')\n\n# Get file sizes and modification dates\nname_sz_date = [(name, os.path.getsize(name), os.path.getmtime(name))\n                for name in pyfiles]\nfor name, size, mtime in name_sz_date:\n    print(name, size, mtime)\n\n# Alternative: Get file metadata\nfile_metadata = [(name, os.stat(name)) for name in pyfiles]\nfor name, meta in file_metadata:\n    print(name, meta.st_size, meta.st_mtime)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u6700\u540e\u8fd8\u6709\u4e00\u70b9\u8981\u6ce8\u610f\u7684\u5c31\u662f\uff0c\u6709\u65f6\u5019\u5728\u5904\u7406\u6587\u4ef6\u540d\u7f16\u7801\u95ee\u9898\u65f6\u5019\u53ef\u80fd\u4f1a\u51fa\u73b0\u4e00\u4e9b\u95ee\u9898\u3002\n\u901a\u5e38\u6765\u8bb2\uff0c\u51fd\u6570 os.listdir() \u8fd4\u56de\u7684\u5b9e\u4f53\u5217\u8868\u4f1a\u6839\u636e\u7cfb\u7edf\u9ed8\u8ba4\u7684\u6587\u4ef6\u540d\u7f16\u7801\u6765\u89e3\u7801\u3002\n\u4f46\u662f\u6709\u65f6\u5019\u4e5f\u4f1a\u78b0\u5230\u4e00\u4e9b\u4e0d\u80fd\u6b63\u5e38\u89e3\u7801\u7684\u6587\u4ef6\u540d\u3002\n\u5173\u4e8e\u6587\u4ef6\u540d\u7684\u5904\u7406\u95ee\u9898\uff0c\u57285.14\u548c5.15\u5c0f\u8282\u6709\u66f4\u8be6\u7ec6\u7684\u8bb2\u89e3\u3002"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.7.1"
    },
    "toc": {
      "base_numbering": 1,
      "nav_menu": {},
      "number_sections": true,
      "sideBar": true,
      "skip_h1_title": true,
      "title_cell": "Table of Contents",
      "title_sidebar": "Contents",
      "toc_cell": false,
      "toc_position": {},
      "toc_section_display": true,
      "toc_window_display": true
    }
  },
  "nbformat": 4,
  "nbformat_minor": 2
}